﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Windows.Forms;
using System.IO;
using DataAccessor.Protector;

namespace DataAccessor
{
    /// <summary>
    /// 缓存所有的TSQL配置并且提供访问接口
    /// </summary>
    public static class TSQLCache
    {
        private static object _syncLock = new object();
        /// <summary>
        /// 开辟缓存区域，缓存所有SQL语句块
        /// </summary>
        private static Dictionary<string, TSQLStatement[]> _statementCache = new Dictionary<string, TSQLStatement[]>();

        /// <summary>
        /// 静态构造函数，调用时构建_sqlCache的实例
        /// </summary>
        static TSQLCache()
        {
            BuildCache();
        }

        /// <summary>
        /// 获取所有TSQL配置
        /// </summary>
        /// <param name="databaseName">模块名称</param>
        /// <param name="configName">配置名称</param>
        /// <returns>配置下所有TSQL语句块的集合</returns>
        public static TSQLStatement[] GetStatementCollection(string databaseName, string configName)
        {
            if (string.IsNullOrEmpty(databaseName))
            {
                throw new ArgumentNullException("必须指定模块名。", databaseName);
            }

            if (string.IsNullOrEmpty(configName))
            {
                throw new ArgumentNullException("必须指定配置名。", configName);
            }

            string cacheKey = string.Format("{0}.{1}", databaseName, configName);
            if (!_statementCache.ContainsKey(cacheKey))
            {
                throw new KeyNotFoundException(string.Format("Holist.xml中没有找到{0}的配置节点。", cacheKey));
            }
            else
            {
                return _statementCache[cacheKey];
            }
        }

        /// <summary>
        /// 构建缓存
        /// </summary>
        private static void BuildCache()
        {
            // 获取Holistic文件路径和文件夹信息
            FileInfo holisticFileInfo = new FileInfo(HolisticCache.HolisticFilePath);
            string rootPath = holisticFileInfo.DirectoryName;

            // 遍历每个模块
            foreach (HolisticDatabase database in HolisticCache.Holistic.Database)
            {
                //创建保护算法的实例

                IProtector protector = HolisticParser.CreateProtector(database.Protector);

                foreach (HolisticDatabaseConfig config in database.Config)
                {
                    // 如果有重复的则只缓存第一个
                    string cacheKey = string.Format("{0}.{1}", database.Name, config.Name);
                    if (!_statementCache.ContainsKey(cacheKey))
                    {
                        // 缓存
                        string tsqlFilePath = Path.Combine(rootPath, config.FilePath);
                        TSQLParser tsqlParser = new TSQLParser(tsqlFilePath, protector);
                        _statementCache.Add(cacheKey, tsqlParser.StatementArray);
                    }
                }
            }
        }
    }
}
